package com.xul.crowd.interceptor;


import com.xul.crowd.constant.CrowdConstant;
import com.xul.crowd.entity.Admin;
import com.xul.crowd.exception.AccessForbiddenException;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author xul
 * @create 2021-07-23 20:09
 */
public class LoginInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        // 1.通过httpServletRequest获取到session对象
        HttpSession session = httpServletRequest.getSession();
        // 2.尝试从session域中获取到Admin对象
        Admin admin = (Admin) session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN);
        // 3.判断对象是否为空
        if (admin == null) {
            throw new AccessForbiddenException(CrowdConstant.MESSAGE_ACCESS_FORBIDDEN);
        }
        // 4.如果admin对象不为空就放行
        return true;
    }


}
